﻿using EnterpriseWebsiteEntity;
using EnterpriseWebsiteUtil.Helper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace EnterpriseWebsiteBLL
{
    public class GlobalConfigService
    {
        public readonly SqlSugarClient _db;

        public GlobalConfigService(SqlSugarHelper sqlSugarHelper)
        {
            _db = sqlSugarHelper.sugarClient;
        }

        public async Task<GlobalConfigEntity> GetGlobalConfigAsync()
        {
            return await _db.Queryable<GlobalConfigEntity>().SingleAsync();
        }

        public bool SaveGlobalConfig(GlobalConfigEntity config)
        {
            var hasRecord = _db.Queryable<GlobalConfigEntity>().First();

            if (hasRecord != null)
            {
                config.Id = hasRecord.Id;
                var result = _db.Updateable(config)
                    .ExecuteCommand();
                return result > 0;
            }
            else
            {
                var result = _db.Insertable(config).ExecuteCommand();
                return result > 0;
            }
        }
    }
}
